package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findBYNameAndPage(ChannelDto dto) {
        //检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //检查分页查询
        dto.checkParam();

        // 封装查询条件
        Page pageRequest = new Page<>(dto.getPage(), dto.getSize());//分页条件
        //查询条件
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(dto.getName())) {
            //根据名称模糊查询
            lambdaQueryWrapper.like(AdChannel::getName, dto.getName());
        }


        //分页查询  1 分页条件 2. 查询条件
        IPage result = page(pageRequest, lambdaQueryWrapper);
        //返回查询结果

        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());

        responseResult.setData(result.getRecords());//分页集合结果返回数据，getRecords()返回所有满足条件的元数据记录结果
        // System.out.println("dto.getSize() = " + dto.getSize());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.检查参数
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.保存
        adChannel.setIsDefault(false);
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.检查参数
        if(adChannel==null || adChannel.getId() ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.执行修改
        updateById(adChannel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }

        //先查询，如果存在，执行删除
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        removeById(id);

        //自己抛异常，参数错误，ID不能大于10
        if(id>10){
            throw new CustomException(ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"Id不可以大于10") );
        }


        return ResponseResult.okResult();
    }
}
